<script setup>
import { getProductionStatus } from "@/api/visual/purchaseBoard";

const totals = ref({});
const props = defineProps({
  // 将变量名改为props（复数形式）
  activeLineID: {
    type: String,
    default: "",
  },
  active: {
    type: String,
    default: "",
  },
});

const loadData = async () => {
  const { data } = await getProductionStatus({
    LineID: props.activeLineID,
    OrderID: props.active,
  });
  if (data.length === 0) {
    optionData.value = {
      GoodQty: 0,
      BadQty: 0,
    };
  } else {
    totals.value = data.reduce((acc, curr) => {
      for (const key in curr) {
        if (typeof curr[key] === "number") {
          acc[key] = (acc[key] || 0) + curr[key];
        }
      }
      return acc;
    }, {});

    console.log(totals.value);
  }
};
watch(
  () => [props.activeLineID, props.active], // 使用正确的变量名props
  async (newValues, oldValues) => {
    if (newValues !== oldValues || newValues !== oldValues) {
      await loadData();
    }
  },
  { immediate: true }
);
onMounted(loadData);
</script>

<template>
  <div class="ps_w_h_100">
    <dv-border-box13>
      <div class="purchaseBoard7-content">
        <div class="title board-title">
          <div style="padding: 10px; font-weight: 500">生产状态</div>
        </div>
        <div
          style="display: flex; justify-content: space-around; margin-top: 10%"
        >
          <div>
            <div class="item-title">目标数</div>
            <div class="item-chart">{{ totals.Quantity }}</div>
          </div>
          <div>
            <div class="item-title">完成数</div>
            <div class="item-chart">{{ totals.GoodQty + totals.BadQty }}</div>
          </div>
          <div>
            <div class="item-title">达成率</div>
            <div class="item-chart">
              {{
                totals.GoodQty + totals.BadQty === totals.Quantity
                  ? "100.00%"
                  : ((totals.GoodQty / totals.Quantity) * 100).toFixed(2) + "%"
              }}
            </div>
          </div>
        </div>
        <div style="display: flex; justify-content: space-around">
          <div>
            <div class="item-title">良品数</div>
            <div class="item-chart">{{ totals.GoodQty }}</div>
          </div>

          <div>
            <div class="item-title">不良数</div>
            <div class="item-chart">{{ totals.BadQty }}</div>
          </div>
          <div>
            <div class="item-title">合格率</div>
            <div class="item-chart">
              {{
                totals.BadQty === 0
                  ? "100.00%"
                  : ((totals.BadQty / totals.GoodQty) * 100).toFixed(2)
              }}
            </div>
          </div>
        </div>
      </div>
    </dv-border-box13>
  </div>
</template>

<style scoped lang="scss">
.purchaseBoard7-content {
  padding: 10px;
  height: 100%;

  .title {
    color: #fff;
    margin-bottom: 10px;
    height: 10%;
  }

  .circle-list {
    margin-top: 10%;
    display: flex;
    justify-content: space-evenly;
  }

  .circle-list-item {
    width: 30%;
    display: flex;
    align-items: center;
    flex-direction: column;
  }

  .item-title {
    color: #fff;
  }
  .item-chart {
    width: 100%;
    height: 70%;
    color: #fff;
  }
}
</style>
